app.controller('sellerController' ,function($scope,$controller,sellerService,indexService){
	
	$controller('baseController',{$scope:$scope});
    //读取列表数据绑定到表单中  
	$scope.findAll=function(){
		sellerService.findAll().success(
			function(response){
				$scope.list=response;
			}			
		);
	}    
	
	//分页
	$scope.findPage=function(page,rows){			
		sellerService.findPage(page,rows).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
	
	//查询实体 
	$scope.findOne=function(id){
		sellerService.findOne(id).success(
			function(response){
				$scope.entity= response;					
			}
		);				
	}
	
	//保存 
	$scope.save=function(){				
		var serviceObject;//服务层对象  				
		if($scope.loginName!=null){//如果有ID
			serviceObject=sellerService.update( $scope.entity ); //修改  
		}else{
			serviceObject=sellerService.add( $scope.entity  );//增加 
		}				
		serviceObject.success(
			function(response){
				if(response.flag){
                    alert(response.message);
					//重新查询 
		        	$scope.reloadList();//重新加载
				}else{
					alert(response.message);
				}
			}		
		);				
	}

		//保存用户注册信息
	 $scope.add=function(){
		//解构表单内的实体属性
		const info={
			...$scope.entity
		}
		console.log(info)
		if (info.sellerId==null||
            info.password==null||
            info.nickName==null||
            info.name==null||
            info.telephone==null||
            info.addressDetail==null||
            info.linkmanName==null||
            info.linkmanQq==null||
            info.linkmanMobile==null||
            info.linkmanEmail==null||
            info.licenseNumber==null||
            info.taxNumber==null||
            info.orgNumber==null||
            info.legalPerson==null||
            info.legalPersonCardId==null||
            info.bankName==null||
            info.bankUser==null){
			alert("表单信息不能为空！")
			return
		 }
		sellerService.add($scope.entity).success(function (response) {
			console.log(response)
			if (response.flag){
                alert(response.message);
				location.href="/shoplogin.html";
			}else {
				alert(response.message);
			}
		})
	 }


	 
	//批量删除 
	$scope.dele=function(){			
		//获取选中的复选框			
		sellerService.dele( $scope.selectIds ).success(
			function(response){
				if(response.success){
					$scope.reloadList();//刷新列表
					$scope.selectIds=[];
				}						
			}		
		);				
	}
	
	$scope.searchEntity={};//定义搜索对象 
	
	//搜索
	$scope.search=function(page,rows){			
		sellerService.search(page,rows,$scope.searchEntity).success(
			function(response){
				$scope.list=response.rows;	
				$scope.paginationConf.totalItems=response.total;//更新总记录数
			}			
		);
	}
    /**
	 * 获取id
     */
    $scope.loginName=function () {
    	const pojo=$scope.getItem()
    	if (pojo.name!=null) {
    		$scope.entity={...pojo}
		}else {
            indexService.loginName()
                .success(
                    function (res) {
                        $scope.loginName=res.loginName
                        $scope.findOne($scope.loginName)
                    }
                )
		}
    }
    /**
	 * 保存按钮--讲信息保存在sessionStorage中
     */
    $scope.saveSessionLocal=function(entity){
    	if (window.sessionStorage){
    		const pojo={...entity}
    		$scope.setItem(pojo)
		}
	}
	$scope.setItem=function (pojo) {
        window.sessionStorage.setItem('name',pojo.name||'')
        window.sessionStorage.setItem('mobile',pojo.mobile||'')
        window.sessionStorage.setItem('telephone',pojo.telephone||'')
        window.sessionStorage.setItem('addressDetail',pojo.addressDetail||'')
        window.sessionStorage.setItem('linkmanName',pojo.linkmanName||'')
        window.sessionStorage.setItem('linkmanQq',pojo.linkmanQq||'')
        window.sessionStorage.setItem('linkmanMobile',pojo.linkmanMobile||'')
        window.sessionStorage.setItem('linkmanEmail',pojo.linkmanEmail||'')
        window.sessionStorage.setItem('licenseNumber',pojo.licenseNumber||'')
        window.sessionStorage.setItem('taxNumber',pojo.taxNumber||'')
        window.sessionStorage.setItem('orgNumber',pojo.orgNumber||'')
        window.sessionStorage.setItem('legalPerson',pojo.legalPerson||'')
        window.sessionStorage.setItem('legalPersonCardId',pojo.legalPersonCardId||'')
        window.sessionStorage.setItem('bankUser',pojo.bankUser||'')
        window.sessionStorage.setItem('bankName',pojo.bankName||'')
        window.sessionStorage.setItem('legalPersonCardId',pojo.legalPersonCardId||'')
    }
    $scope.getItem=function () {
		const pojo={
            name:window.sessionStorage.getItem('name'),
            mobile:window.sessionStorage.getItem('mobile'),
            telephone:window.sessionStorage.getItem('telephone'),
            addressDetail:window.sessionStorage.getItem('addressDetail'),
            linkmanName:window.sessionStorage.getItem('linkmanName'),
            linkmanQq:window.sessionStorage.getItem('linkmanQq'),
            linkmanMobile:window.sessionStorage.getItem('linkmanMobile'),
            linkmanEmail:window.sessionStorage.getItem('linkmanEmail'),
            licenseNumber:window.sessionStorage.getItem('licenseNumber'),
            taxNumber:window.sessionStorage.getItem('taxNumber'),
            orgNumber:window.sessionStorage.getItem('orgNumber'),
            legalPerson:window.sessionStorage.getItem('legalPerson'),
            legalPersonCardId:window.sessionStorage.getItem('legalPersonCardId'),
            bankUser:window.sessionStorage.getItem('bankUser'),
            bankName:window.sessionStorage.getItem('bankName'),
            legalPersonCardId:window.sessionStorage.getItem('legalPersonCardId')
		}
		return pojo;
    }

    /**
	 * 修改密码对象
     */
    $scope.pwdEntity={}

    /**
	 * 密码保存按钮
     */
    $scope.savePassword=function () {
    	if ($scope.pwdEntity.oldPassword==$scope.pwdEntity.newPassword) {
    		alert('新老密码相同！修改失败！')
			return;
		}
		if ($scope.pwdEntity.newPassword!=$scope.pwdEntity.newPasswordAgain) {
			alert('两次密码输入不相同！修改失败!')
			return
		}
        window.sessionStorage.setItem('newPassword',$scope.pwdEntity.newPassword||'')
        window.sessionStorage.setItem('newPasswordAgain',$scope.pwdEntity.newPasswordAgain||'')
        window.sessionStorage.setItem('oldPassword',$scope.pwdEntity.oldPassword||'')
    }

    /**
	 * 按下保存按钮初始化
     */
	$scope.initPwd=function () {
		$scope.pwdEntity.newPassword=window.sessionStorage.getItem('newPassword')
        $scope.pwdEntity.newPasswordAgain=window.sessionStorage.getItem('newPasswordAgain')
        $scope.pwdEntity.oldPassword=window.sessionStorage.getItem('oldPassword')
    }
    /**
	 * 更新密码
     */
    $scope.updatePassword=function () {
		sellerService.updatePassword($scope.pwdEntity.oldPassword,$scope.pwdEntity.newPassword)
			.success(function (res) {
				if (res.flag){
					alert(res.message)
                    window.sessionStorage.removeItem('newPassword')
                    window.sessionStorage.removeItem('newPasswordAgain')
                    window.sessionStorage.removeItem('oldPassword')
                    window.location.reload()
                    parent.window.location.href='/shoplogin.html'
				}
            })
    }
});